package mysql

import (
	"fmt"
	"gorm.io/gorm"
	"week3/models/global"
	"week3/models/request"
)

// 课程表
type ShopKe struct {
	gorm.Model
	KeFenLeId   int       `gorm:"type:int(10); comment:课程分类;"`
	KeFenLe     KeFenLe   `gorm:"foreignKey:KeFenLeId"` //绑定外键
	KeFanXianId int       `gorm:"type:int(10);comment:课程方向;"`
	KeFanXian   KeFanXian `gorm:"foreignKey:KeFanXianId;"` //绑定外键
	ShopTitle   string    `gorm:"type:varchar(30); comment:课程标题;index;"`
	ShopPrice   float64   `gorm:"type:decimal(10,2); comment:课程价格;"`
	ShopNum     int       `gorm:"type:int(10); comment:课程已购买的数量;"`
	IsGoWucId   int       `gorm:"type:int(10); comment:是否加入购物车 1是 0否;"`
	GoWuc       GoWuc     `gorm:"foreignKey:IsGoWucId;"` //绑定外键
	DianZan     int       `gorm:"type:int(10);comment:点赞数;"`
}

// 添加课程
func CreateKe(data request.ShopKeRequest) bool {
	ke := ShopKe{
		KeFenLeId: data.KeFenLeId,
		KeFenLe: KeFenLe{
			FenLeName: "测试",
			Pid:       0,
		},
		KeFanXianId: data.KeFanXianId,
		KeFanXian: KeFanXian{
			FanXianName: "测试",
			Pid:         0,
		},
		ShopTitle: data.ShopTitle,
		ShopPrice: data.ShopPrice,
		ShopNum:   data.ShopNum,
		IsGoWucId: data.IsGoWucId,
		GoWuc: GoWuc{
			ShopTitle: data.ShopTitle,
			Num:       data.ShopNum,
			Price:     data.ShopPrice,
		},
		DianZan: data.DianZan,
	}
	err := global.DB.Create(&ke).Error
	if err != nil {
		return false
	}
	return true
}

// 列表查询
func KeList(data request.KeListRequest) []ShopKe {
	var keshop []ShopKe
	global.DB.Where("shop_title like ?", "%"+data.KeyWord+"%").Find(&keshop)
	fmt.Println(keshop)
	return keshop
}

// 联查
func GetKeDetail() []ShopKe {
	var keList []ShopKe
	global.DB.Preload("KeFenLe").Preload("KeFanXian").Preload("GoWuc").Find(&keList)
	return keList
}
